﻿//https://leetcode.cn/problems/pascals-triangle/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    vector<vector<int>> generate(int numRows)
    {
        vector<vector<int>> vv(numRows);
        for (int i = 0; i < numRows; i++)
        {
            vv[i].resize(i + 1, 1);
        }

        for (int i = 2; i < numRows; i++)
        {
            for (int j = 1; j < vv[i].size() - 1; j++)
            {
                vv[i][j] = vv[i - 1][j - 1] + vv[i - 1][j];
            }
        }

        return vv;
    }
};

//https://leetcode.cn/problems/house-robber/submissions/619511428/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int rob(vector<int>& nums)
    {
        if (nums.empty()) return 0;
        if (nums.size() == 1) return nums[0];
        vector<int> dp(nums.size());
        dp[0] = nums[0];
        dp[1] = max(nums[0], nums[1]);
        for (int i = 2; i < nums.size(); i++)
        {
            dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]);
        }

        return dp[nums.size() - 1];
    }
};

//https://leetcode.cn/problems/perfect-squares/submissions/619516040/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int numSquares(int n)
    {
        int m = sqrt(n);
        vector<int> dp(n + 1);
        for (int i = 1; i <= n; i++) dp[i] = 0x3f3f;

        for (int i = 1; i <= m; i++)
        {
            for (int j = i * i; j <= n; j++)
            {
                dp[j] = min(dp[j], dp[j - i * i] + 1);
            }
        }

        return dp[n];
    }
};

//https://leetcode.cn/problems/coin-change/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int coinChange(vector<int>& coins, int amount)
    {
        vector<int> dp(amount + 1, 0x3f3f3f);
        dp[0] = 0;
        for (int i = 1; i <= coins.size(); i++)
        {
            for (int j = coins[i - 1]; j <= amount; j++)
            {
                dp[j] = min(dp[j], dp[j - coins[i - 1]] + 1);
            }
        }

        return dp[amount] == 0x3f3f3f ? -1 : dp[amount];
    }
};
